package com.android.thelastnoet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.strictmode.Violation;
import android.service.quicksettings.TileService;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LockActivity extends AppCompatActivity {

    String TAG = "LockActivity";
    /**
     * 输入
     */
    String editStr = "";
    /**
     * 密码
     */
    String lockPwd = "";
    /**
     * 验证密码
     */
    String setVerPed = "";

    TextView ties_tv;

    SharedPreferences pref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);

        pref = getSharedPreferences("data", MODE_PRIVATE);

        ties_tv = findViewById(R.id.ties_tv);
        Toolbar toolbar = findViewById(R.id.toolbar_lock);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(v -> {
            Intent intent = new Intent(LockActivity.this, MainActivity.class);
            startActivity(intent);
        });

        chooseType();

    }

    public void onClick(View view) {
        Button button = (Button) view;
        switch (button.getId()) {
            case R.id.btn_forget:
                Toast.makeText(this, "暂未开发", Toast.LENGTH_SHORT).show();
            case R.id.btn_back:
                if (TextUtils.isEmpty(editStr)) {
                    finish();
                    break;
                } else {
                    editStr = editStr.substring(0, editStr.length() - 1);
                }
            default:
                String btn_text = (String) button.getText();
                if (!btn_text.equals("删除")) {
                    editStr += btn_text;
                }
/*
                Log.d(TAG, editStr + "/length:" + editStr.length() + "/" + btn_text);
*/
                if (editStr.length() == 4) {
                    if (TextUtils.isEmpty(lockPwd)) {
                        setLockPwd(editStr);
                    } else {
                        verifyLockPwd();
                    }
                }
                break;
        }
    }

    /*设置密码*/
    private void setLockPwd(String str) {
        if (TextUtils.isEmpty(setVerPed)) {
            setVerPed = str;
            editStr = "";
            Log.d(TAG, editStr);
            ties_tv.setText(R.string.verLockPwd);
        } else {
            Log.d(TAG, str+" | "+setVerPed);
            if (setVerPed.equals(str)) {
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                editor.putString("LockPwd", setVerPed);
                editor.apply();
                Toast.makeText(this, "设置密码成功！", Toast.LENGTH_SHORT).show();
                ties_tv.setText(R.string.LockTies);
                editStr="";
                setVerPed="";
                Intent intent = new Intent(LockActivity.this, NoteListActivity.class);
                intent.putExtra("groupName","私密便签");
                startActivity(intent);
            } else {
                Toast.makeText(this, "两次密码不相同！请重新设置", Toast.LENGTH_SHORT).show();
                editStr = "";
                setVerPed = "";
                ties_tv.setText(R.string.setLockPwd);
            }
        }
    }

    /*验证密码*/
    private void verifyLockPwd() {
        if (editStr.equals(lockPwd)) {
            editStr = "";
            Intent intent = new Intent(LockActivity.this, NoteListActivity.class);
            intent.putExtra("groupName","私密便签");
            startActivity(intent);
        } else if (editStr.length() == 4) {
            Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
            editStr = "";
        }
    }

    private void chooseType(){
        lockPwd = pref.getString("LockPwd", "");
        Log.d(TAG, "?"+lockPwd);

        if (TextUtils.isEmpty(lockPwd) && TextUtils.isEmpty(setVerPed)) {
            Log.d(TAG, "设置密码");
            ties_tv.setText(R.string.setLockPwd);
        } else if (TextUtils.isEmpty(lockPwd) && !TextUtils.isEmpty(setVerPed)) {
            Log.d(TAG, "确定密码");
            ties_tv.setText(R.string.verLockPwd);
        } else {
            Log.d(TAG, "验证密码");
            ties_tv.setText(R.string.LockTies);
        }
    }
    @Override
    protected void onResume() {
        Log.d(TAG,"onResume");
        chooseType();
        super.onResume();
    }
}